package com.bonc.ioc.controller;

import com.alibaba.fastjson.JSON;
import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.service.impl.CollectionServiceImpl;
import com.bonc.ioc.util.CurrentUser;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.CollectionVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhangXu
 * @date 2021/3/17 16:22
 */
@RestController
@RequestMapping("/collection")
public class CollectionController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CollectionServiceImpl.class);

    @Autowired
    private CollectionServiceImpl collectionService;

    @Autowired
    private CurrentUser currentUser;

    /**
     * 添加收藏接口
     *
     * @param param 用户id, 资源类型, 资源id
     * @return 添加结果
     */
    @PostMapping("/addCollection")
    @AppAuthAnnotation
    public AppReply addCollection(@RequestBody CollectionVo param) {
        log.info("收藏 添加收藏 接收参数={}", JSON.toJSONString(param));
        Integer count = 0;
        if (StringUtils.isNotBlank(param.getUserId()) || StringUtils.isNotBlank(param.getResourceId().toString())) {
            count = collectionService.addCollection(param);
        }
        if (count > 0) {
            return AppReply.success("添加成功");
        }
        return AppReply.error("添加失败");
    }

    /**
     * 查询收藏列表接口
     *
     * @param param 用户id, 关键字, 查询时段
     * @return 收藏分页列表
     * @throws Exception
     */
    @PostMapping("/queryCollection")
    public AppReply queryCollection(@RequestBody CollectionVo param) throws Exception {
        log.info("收藏 获取收藏列表 接收参数={}", JSON.toJSONString(param));
        PageResult result = collectionService.queryCollection(param);
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", result);
    }

    /**
     * 查询资源是否已经收藏
     *
     * @param userId 用户id
     * @param id     数据id
     * @return 0, 1
     */
    @GetMapping("queryIsCollection")
    public AppReply queryIsCollection(String userId, Long id) {
        log.info("收藏 获取是否收藏 接收参数 用户={}, 资源={}", userId, id);
        Integer isCollection = collectionService.queryIsCollection(userId, id);
        log.info("收藏 获取是否收藏 isCollection={}, 收藏情况={}", isCollection, isCollection > 0 ? "已收藏" : "未收藏");
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", isCollection);
    }

    /**
     * 取消收藏接口
     *
     * @param param 收藏记录id, 用户id
     * @return 取消结果
     */
    @PostMapping("/deleteCollection")
    @AppAuthAnnotation
    public AppReply deleteCollection(@RequestBody CollectionVo param) {
        log.info("收藏 取消收藏 接收参数={}", JSON.toJSONString(param));
        Integer count = collectionService.deleteCollection(param);
        if (count > 0) {
            return AppReply.success("取消成功");
        }
        return AppReply.error("取消失败");
    }
}
